 
 
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>镜像站@树形组件文档 - Layui</title>
<meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="apple-mobile-web-app-status-bar-style" content="black"> 
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="format-detection" content="telephone=no">
  <script>
  ;!function(){self!==parent&&(location.href="http://www.baidu.com/")}();
  </script>
  <link rel="stylesheet" href="../../../res.layui.com/layui/dist/css/layui.css" tppabs="http://res.layui.com/layui/dist/css/layui.css?t=1632428048260"  media="all">
  <link rel="stylesheet" href="../../../res.layui.com/static/css/global.css" tppabs="http://res.layui.com/static/css/global.css?t=1632428048260-19" media="all">
</head>
<body>


<div class="layui-header header header-doc" autumn>
  <div class="layui-container">
    <a class="logo" href="../../index.htm" tppabs="http://www.layui.com/">
      <img src="../../../res.layui.com/static/images/layui/logo.png" tppabs="http://res.layui.com/static/images/layui/logo.png" alt="layui">
    </a>
    <div class="layui-form layui-hide-xs component" lay-filter="LAY-site-header-component"></div>
    <div class="layui-hide-xs site-notice"></div>
    
    <ul class="layui-nav" id="LAY_NAV_TOP">
      <li class="layui-nav-item layui-this">
        <a href="../index.htm" tppabs="http://www.layui.com/doc/">文档</a> 
      </li>
      <li class="layui-nav-item ">
        <a href="../../demo/index.htm" tppabs="http://www.layui.com/demo/">示例</a>
      </li>
      
      <li class="layui-nav-item">
        <a href="javascript:;">
          <!--<span class="layui-badge-dot" style="left:0; right: auto; margin: -4px 0 0 5px;"></span>--> 
          周边
        </a>
        <dl class="layui-nav-child layui-nav-child-c">  
          <dd class="layui-hide-sm layui-show-xs" lay-unselect>
            <a href="https://gitee.com/sentsin/layui/issues" target="_blank" rel="nofollow">问题反馈</a>
            <hr>
          </dd>
          
          <dd lay-unselect><a href="../../alone.html" tppabs="http://www.layui.com/alone.html" target="_blank" lay-unselect>独立组件</a></dd>
          <dd lay-unselect><a href="../../../fly.layui.com/extend/index.htm" target="_blank">扩展组件</a></dd>
        </dl>
      </li>
      
      <li class="layui-nav-item layui-hide-xs">
        <a href="http://gitee.com/sentsin/layui/issues" target="_blank" rel="nofollow">反馈</a>
      </li>
    </ul>
  </div>
</div>
 
<!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
<!--[if lt IE 9]>
  <script src="../../../other/html5shiv/r29/html5.min.js" tppabs="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <script src="../../../other/respond.js/1.4.2/respond.min.js" tppabs="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
 
<ul class="site-dir">
  <li><a href="#use"><cite>快速使用</cite></a></li>
  <li><a href="#options"><cite>基础参数</cite></a></li>
  <li><a href="#data"><cite>数据源参数</cite></a></li>
  <li>回调</li>
  <ul style="margin-left: 15px;">
    <li><a href="#onclick">节点被点击的回调</a></li>
    <li><a href="#onCheckbox">复选框被点击的回调</a></li>
    <!--
    <li><a href="#onSearch">节点搜索完毕的回调</a></li>
    <li><a href="#onDragStart">开始拖拽的回调</a></li>
    <li><a href="#onDragEnd">拖拽完成的回调</a></li>
    -->
    <li><a href="#operate">操作节点的回调</a></li>
  </ul>
  <li>方法</li>
  <ul style="margin-left: 15px;">
    <li><a href="#getChecked">返回选中的节点数据</a></li>
    <li><a href="#setChecked">设置节点勾选</a></li>
    <li><a href="#reload">实例重载</a></li>
  </ul>
</ul>
<div class="layui-container layui-row">
  
<div class="layui-col-md3">
  <div class="layui-panel site-menu">
    <ul class="layui-menu layui-menu-lg">
      <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
        <div class="layui-menu-body-title">
          基础说明
        </div>
        <hr>
        <ul>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../index.htm" tppabs="http://www.layui.com/doc/">
                <span>开始使用 </span> 
                <span class="layui-font-12 layui-font-gray">Getting Started</span>
              </a>
              
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/infrastructure.html" tppabs="http://www.layui.com/doc/base/infrastructure.html">
                <span>底层方法 </span>
                <span class="layui-font-12 layui-font-gray">基础支撑</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/element.html" tppabs="http://www.layui.com/doc/base/element.html">
                <span>页面元素 </span>
                <span class="layui-font-12 layui-font-gray">规范 公共类 属性</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/modules.html" tppabs="http://www.layui.com/doc/base/modules.html">
                <span>模块规范 </span>
                <span class="layui-font-12 layui-font-gray">使用 扩展</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/faq.html" tppabs="http://www.layui.com/doc/base/faq.html">
                <span>常见问题 </span>
                <span class="layui-font-12 layui-font-gray">FAQ</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/changelog.html" tppabs="http://www.layui.com/doc/base/changelog.html">
                <span>更新日志 </span>
                <span class="layui-font-12 layui-font-gray">changelog</span>
                
                  <span class="layui-badge-dot"></span>
                
              </a>
            </div>
          </li>
        </ul>
      </li>
      
      <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
        <div class="layui-menu-body-title">
          页面元素
        </div>
        <hr>
        <ul>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/layout.html" tppabs="http://www.layui.com/doc/element/layout.html">
                <span>布局 </span>
                <span class="layui-font-12 layui-font-gray">栅格 后台管理布局</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/color.html" tppabs="http://www.layui.com/doc/element/color.html">
                <span>颜色 </span>
                <span class="layui-font-12 layui-font-gray">主色调 颜色搭配</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/icon.html" tppabs="http://www.layui.com/doc/element/icon.html">
                <span>图标 </span>
                <span class="layui-font-12 layui-font-gray">iconfont 字体图标</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/anim.html" tppabs="http://www.layui.com/doc/element/anim.html">
                <span>动画 </span>
                <span class="layui-font-12 layui-font-gray">内置 CSS3 动画</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/button.html" tppabs="http://www.layui.com/doc/element/button.html">
                <span>按钮 </span>
                <span class="layui-font-12 layui-font-gray">button</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/form.html" tppabs="http://www.layui.com/doc/element/form.html">
                <span>表单 </span>
                <span class="layui-font-12 layui-font-gray">form 元素集合</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/nav.html" tppabs="http://www.layui.com/doc/element/nav.html">
                <span>导航 </span>
                <span class="layui-font-12 layui-font-gray">nav 面包屑</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/menu.html" tppabs="http://www.layui.com/doc/element/menu.html">
                <span>菜单 </span>
                <span class="layui-font-12 layui-font-gray">menu 基础菜单</span>
                
                  <span class="layui-badge-dot"></span>
                
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/tab.html" tppabs="http://www.layui.com/doc/element/tab.html">
                <span>选项卡 </span>
                <span class="layui-font-12 layui-font-gray">Tabs 切换</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/progress.html" tppabs="http://www.layui.com/doc/element/progress.html">
                <span>进度条 </span>
                <span class="layui-font-12 layui-font-gray">progress</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/panel.html" tppabs="http://www.layui.com/doc/element/panel.html">
                <span>面板 </span>
                <span class="layui-font-12 layui-font-gray">panel 卡片 折叠</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/table.html" tppabs="http://www.layui.com/doc/element/table.html">
                <span>表格 </span>
                <span class="layui-font-12 layui-font-gray">静态 table</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/badge.html" tppabs="http://www.layui.com/doc/element/badge.html">
                <span>徽章 </span>
                <span class="layui-font-12 layui-font-gray">小圆点  小边框</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/timeline.html" tppabs="http://www.layui.com/doc/element/timeline.html">
                <span>时间线 </span>
                <span class="layui-font-12 layui-font-gray">timeline</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/auxiliar.html" tppabs="http://www.layui.com/doc/element/auxiliar.html">
                <span>辅助 </span>
                <span class="layui-font-12 layui-font-gray">引用 字段集 横线等</span>
              </a>
            </div>
          </li>
        </ul>
      </li>
      
      <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
        <div class="layui-menu-body-title">
          内置模块
        </div>
        <hr>
        <ul>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="layer.html" tppabs="http://www.layui.com/doc/modules/layer.html">
                <span>弹出层 </span>
                <span class="layui-font-12 layui-font-gray">layer</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="laydate.html" tppabs="http://www.layui.com/doc/modules/laydate.html">
                <span>日期与时间选择 </span>
                <span class="layui-font-12 layui-font-gray">laydate</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="laypage.html" tppabs="http://www.layui.com/doc/modules/laypage.html">
                <span>分页 </span>
                <span class="layui-font-12 layui-font-gray">laypage</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="laytpl.html" tppabs="http://www.layui.com/doc/modules/laytpl.html">
                <span>模板引擎 </span>
                <span class="layui-font-12 layui-font-gray">laytpl</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="table.html" tppabs="http://www.layui.com/doc/modules/table.html">
                <span>数据表格 </span>
                <span class="layui-font-12 layui-font-gray">table</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="form.html" tppabs="http://www.layui.com/doc/modules/form.html">
                <span>表单 </span>
                <span class="layui-font-12 layui-font-gray">form</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="upload.html" tppabs="http://www.layui.com/doc/modules/upload.html">
                <span>文件上传 </span>
                <span class="layui-font-12 layui-font-gray">upload</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="dropdown.html" tppabs="http://www.layui.com/doc/modules/dropdown.html">
                <span>下拉菜单 </span>
                <span class="layui-font-12 layui-font-gray">dropdown</span>
                
                  <span class="layui-badge-dot"></span>
                
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="transfer.html" tppabs="http://www.layui.com/doc/modules/transfer.html">
                <span>穿梭框 </span>
                <span class="layui-font-12 layui-font-gray">transfer</span>
              </a>
            </div>
          </li>
          <li class="layui-menu-item-checked2">
            <div class="layui-menu-body-title">
              <a href="tree.html" tppabs="http://www.layui.com/doc/modules/tree.html">
                <span>树形组件 </span>
                <span class="layui-font-12 layui-font-gray">tree</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="colorpicker.html" tppabs="http://www.layui.com/doc/modules/colorpicker.html">
                <span>颜色选择器 </span>
                <span class="layui-font-12 layui-font-gray">colorpicker</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="element.html" tppabs="http://www.layui.com/doc/modules/element.html">
                <span>常用元素操作 </span>
                <span class="layui-font-12 layui-font-gray">element</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="slider.html" tppabs="http://www.layui.com/doc/modules/slider.html">
                <span>滑块 </span>
                <span class="layui-font-12 layui-font-gray">slider</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="rate.html" tppabs="http://www.layui.com/doc/modules/rate.html">
                <span>评分 </span>
                <span class="layui-font-12 layui-font-gray">rate</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="carousel.html" tppabs="http://www.layui.com/doc/modules/carousel.html">
                <span>轮播 </span>
                <span class="layui-font-12 layui-font-gray">carousel</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="flow.html" tppabs="http://www.layui.com/doc/modules/flow.html">
                <span>流加载 </span>
                <span class="layui-font-12 layui-font-gray">flow</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="util.html" tppabs="http://www.layui.com/doc/modules/util.html">
                <span>工具组件 </span>
                <span class="layui-font-12 layui-font-gray">util</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="code.html" tppabs="http://www.layui.com/doc/modules/code.html">
                <span>代码高亮显示 </span>
                <span class="layui-font-12 layui-font-gray">code</span>
              </a>
            </div>
          </li>
        </ul>
      </li>
    </ul>
  </div>
  <div class="layui-hide-v"> - </div>
</div>

  
  <div class="layui-col-md9 site-content">
    <h1 class="site-h1"><i class="layui-icon">&#xe62e;</i> 树形组件文档 - layui.tree</h1>
    <div class="site-tips site-text">
      <p>在一段漫长的雪藏后，我们在 layui 2.5.0 的版本中重新创作了 tree，以便它能够更加适用于绝大多数业务场景，而风格依然遵循 layui 独有的极简和清爽。需要提醒的是，如果您的项目中仍然采用了 layui 2.5 版本之前的 tree，它将不被兼容，请尽快修改为以下新的调用方式。</p>
    </div>
    <div class="site-tips site-text">
      <p>模块加载名称：<em>tree</em></p>
    </div>
    
    
    
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="use">快速使用</a></legend>
    </fieldset>
    <div class="site-text">
      <p>通过 <em>tree.render()</em> 方法指定一个元素，便可快速创建一个 tree 实例。</p>
      <pre class="layui-code">
&lt;!DOCTYPE html>
&lt;html>
&lt;head>
  &lt;meta charset="utf-8">
  &lt;title>树组件&lt;/title>
  &lt;link rel="stylesheet" href="../src/css/layui.css">
&lt;/head>
&lt;body>
  &lt;div id="test1">&lt;/div>
  &lt;script src="../src/layui.js">&lt;/script>
  &lt;script>
  layui.use('tree', function(){
    var tree = layui.tree;
   
    //渲染
    var inst1 = tree.render({
      elem: '#test1'  //绑定元素
      ,data: [{
        title: '江西' //一级菜单
        ,children: [{
          title: '南昌' //二级菜单
          ,children: [{
            title: '高新区' //三级菜单
            //…… //以此类推，可无限层级
          }]
        }]
      },{
        title: '陕西' //一级菜单
        ,children: [{
          title: '西安' //二级菜单
        }]
      }]
    });
  });
  &lt;/script>
&lt;/body>
&lt;/html>
      </pre>
    </div> 
    
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="options">基础参数</a></legend>
    </fieldset>
    <div class="site-text">
      <p>目前 tree 组件提供以下基础参数，可根据需要进行相应的设置</p>
      <table class="layui-table">
        <colgroup>
          <col width="120">
          <col>
          <col width="120">
          <col width="200">
        </colgroup>
        <thead>
          <tr>
            <th>参数选项</th>
            <th>说明</th>
            <th>类型</th>
            <th>示例值</th>
          </tr> 
        </thead>
        <tbody>
          <tr>
            <td>elem</td>
            <td>指向容器选择器</td>
            <td>String/Object</td>
            <td>-</td>
          </tr>
          <tr>
            <td>data</td>
            <td>数据源</td>
            <td>Array</td>
            <td>
              <a href="#data">详见数据选项</a>
            </td>
          </tr>
          <tr>
            <td>id</td>
            <td>设定实例唯一索引，用于基础方法传参使用。</td>
            <td>String</td>
            <td>-</td>
          </tr>
          <tr>
            <td>showCheckbox</td>
            <td>是否显示复选框</td>
            <td>Boolean</td>
            <td>false</td>
          </tr>
          <tr>
            <td>edit</td>
            <td class="layui-text">
              是否开启节点的操作图标。默认 false。
              <ul>
                <li>若为 true，则默认显示“改删”图标</li>
                <li>
                  若为 数组，则可自由配置操作图标的显示状态和顺序，目前支持的操作图标有：<em>add</em>、<em>update</em>、<em>del</em>，如：
                  <br><br><span>edit: ['add', 'update', 'del']</span>
                </li>
              </ul>
            </td>
            <td>Boolean/Array</td>
            <td>['update', 'del']</td>
          </tr>
          <tr>
            <td>accordion</td>
            <td>是否开启手风琴模式，默认  false</td>
            <td>Boolean</td>
            <td>false</td>
          </tr>
          <tr>
            <td>onlyIconControl</td>
            <td>是否仅允许节点左侧图标控制展开收缩。默认 false（即点击节点本身也可控制）。若为 true，则只能通过节点左侧图标来展开收缩</td>
            <td>Boolean</td>
            <td>false</td>
          </tr>
          <tr>
            <td>isJump</td>
            <td>是否允许点击节点时弹出新窗口跳转。默认 false，若开启，需在节点数据中设定 link 参数（值为 url 格式）</td>
            <td>Boolean</td>
            <td>false</td>
          </tr>
          <!--<tr>
            <td>showSearch</td>
            <td>是否打开节点搜索功能。默认 false</td>
            <td>Boolean</td>
            <td>false</td>
          </tr>
          <tr>
            <td>drag</td>
            <td>是否开启节点拖拽。默认 false。</td>
            <td>Boolean</td>
            <td>false</td>
          </tr>
          -->
          <tr>
            <td>showLine</td>
            <td>是否开启连接线。默认 true，若设为 false，则节点左侧出现三角图标。</td>
            <td>Boolean</td>
            <td>true</td>
          </tr>
          <tr>
          <td>text</td>
          <td>
            自定义各类默认文本，目前支持以下设定：
            <pre class="layui-code">
text: {
  defaultNodeName: '未命名' //节点默认名称
  ,none: '无数据' //数据为空时的提示文本
}             
            </pre>
          </td>
          <td>Object</td>
          <td>-</td>
        </tbody>
      </table>
    </div>
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="data">数据源属性选项</a></legend>
    </fieldset>
    <div class="site-text">
      <p>我们将 data 参数称之为数据源，其内部支持设定以下选项</p>
      <table class="layui-table">
        <colgroup>
          <col width="120">
          <col>
          <col width="120">
          <col width="200">
        </colgroup>
        <thead>
          <tr>
            <th>属性选项</th>
            <th>说明</th>
            <th>类型</th>
            <th>示例值</th>
          </tr> 
        </thead>
        <tbody>
          <tr>
            <td>title</td>
            <td>节点标题</td>
            <td>String</td>
            <td>未命名</td>
          </tr>
          <tr>
            <td>id</td>
            <td>节点唯一索引值，用于对指定节点进行各类操作</td>
            <td>String/Number</td>
            <td>任意唯一的字符或数字</td>
          </tr>
          <tr>
            <td>field</td>
            <td>节点字段名</td>
            <td>String</td>
            <td>一般对应表字段名</td>
          </tr>
          <tr>
            <td>children</td>
            <td>子节点。支持设定选项同父节点</td>
            <td>Array</td>
            <td>[{title: '子节点1', id: '111'}]</td>
          </tr>
          <tr>
            <td>href</td>
            <td>点击节点弹出新窗口对应的 url。需开启 isJump 参数</td>
            <td>String</td>
            <td>任意 URL</td>
          </tr>
          <tr>
            <td>spread</td>
            <td>节点是否初始展开，默认 false</td>
            <td>Boolean</td>
            <td>true</td>
          </tr>
          <tr>
            <td>checked</td>
            <td>节点是否初始为选中状态（如果开启复选框的话），默认 false</td>
            <td>Boolean</td>
            <td>true</td>
          </tr>
          <tr>
            <td>disabled</td>
            <td>节点是否为禁用状态。默认 false</td>
            <td>Boolean</td>
            <td>false</td>
          </tr>
        </tbody>
      </table>
    </div>
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="onclick">节点被点击的回调</a></legend>
    </fieldset>
    <div class="site-text">
      <p>在节点被点击后触发，返回的参数如下：</p>
      <pre class="layui-code" lay-title="例子">
tree.render({
  elem: '#test1'
  ,click: function(obj){
    console.log(obj.data); //得到当前点击的节点数据
    console.log(obj.state); //得到当前节点的展开状态：open、close、normal
    console.log(obj.elem); //得到当前节点元素
    
    console.log(obj.data.children); //当前节点下是否有子节点
  }
});
      </pre>
    </div>
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="onCheckbox">复选框被点击的回调</a></legend>
    </fieldset>
    <div class="site-text">
      <p>点击复选框时触发，返回的参数如下：</p>
      <pre class="layui-code" lay-title="例子">
tree.render({
  elem: '#test1'
  ,oncheck: function(obj){
    console.log(obj.data); //得到当前点击的节点数据
    console.log(obj.checked); //得到当前节点的展开状态：open、close、normal
    console.log(obj.elem); //得到当前节点元素
  }
});
      </pre>
    </div>
    
    <!--
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="onSearch">节点搜索完毕的回调</a></legend>
    </fieldset>
    <div class="site-text">
      <p>依赖于 showSearch 参数，返回匹配值的节点名及对应节点，可显示总数</p>
      <pre class="layui-code" lay-title="例子">
tree.render({
  elem: '#test1'
  ,onsearch: function(){
    
  }
});
      </pre>
    </div>
    
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="onDragStart">开始拖拽的回调</a></legend>
    </fieldset>
    <div class="site-text">
      <p>通过 dragstart 实现函数，组件开始拖拽时触发，返回被拖拽节点和父级节点</p>
      <pre class="layui-code" lay-title="例子">
tree.render({
  elem: '#test1'
  ,dragstart: function(obj){
    console.log(obj, parent);
  }
});
      </pre>
    </div>
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="onDragEnd">拖拽完成的回调</a></legend>
    </fieldset>
    <div class="site-text">
      <p>通过 dragend 实现函数，组件拖拽完成后触发，返回拖拽结果，拖拽节点及目标节点</p>
      <pre class="layui-code" lay-title="例子">
tree.render({
  elem: '#test1'
  ,dragend: function(state, obj, target){
    console.log(state);
  }
});
      </pre>
    </div>
    -->
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="operate">操作节点的回调</a></legend>
    </fieldset>
    <div class="site-text">
      <p>通过 operate 实现函数，对节点进行增删改等操作时，返回操作类型及操作节点</p>
      <pre class="layui-code" lay-title="例子">
tree.render({
  elem: '#test1'
  ,operate: function(obj){
    var type = obj.type; //得到操作类型：add、edit、del
    var data = obj.data; //得到当前节点的数据
    var elem = obj.elem; //得到当前节点元素
    
    //Ajax 操作
    var id = data.id; //得到节点索引
    if(type === 'add'){ //增加节点
      //返回 key 值
      return 123;
    } else if(type === 'update'){ //修改节点
      console.log(elem.find('.layui-tree-txt').html()); //得到修改后的内容
    } else if(type === 'del'){ //删除节点
      
    };
  }
});
      </pre>
    </div>
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="getChecked">返回选中的节点数据</a></legend>
    </fieldset>
    <div class="site-text">
      <p>
        很多时候 tree 可能不仅仅只是一个树菜单，它还用于各种权限控制等场景，这个时候你需要获得选中的节点。
        <br>语法：<em>tree.getChecked(id)</em>
      </p>
      <pre class="layui-code" lay-title="例子">
tree.render({
  elem: '#test'
  ,data: [] //数据源
  ,id: 'demoId' //定义索引
});
 
//获得选中的节点
var checkData = tree.getChecked('demoId');
      </pre>
    </div>
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="setChecked">设置节点勾选</a></legend>
    </fieldset>
    <div class="site-text">
      <p>
        除了通过 checked 参数对节点进行初始勾选之外，你还可以通过方法动态对节点执行勾选
        <br>语法：<em> tree.setChecked(id, checkedId)</em>  <br>参数 <em>checkedId</em>：代表的是数据源中的节点 id
      </p>
      <pre class="layui-code" lay-title="例子">
tree.render({
  elem: '#test'
  ,data: [] //数据源
  ,id: 'demoId' //定义索引
});
 
//执行节点勾选
tree.setChecked('demoId', 1); //单个勾选 id 为 1 的节点
tree.setChecked('demoId', [2, 3]); //批量勾选 id 为 2、3 的节点
      </pre>
    </div>
    
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="reload">实例重载</a></legend>
    </fieldset>
    <div class="site-text">
      <p>重载一个已经创建的组件实例，被覆盖新的基础属性</p>
      <pre class="layui-code" lay-title="例子">
tree.render({
  elem: '#test'
  ,data: []
  ,id: 'demoId' //定义索引
});
 
 //可以重载所有基础参数
tree.reload('demoId', {
  //新的参数
});
      </pre>
    </div>
    
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="docend">结语</a></legend>
    </fieldset>
    <div class="site-text">
      <p>树组件还在持续完善。</p>
    </div>
    
    <div class="layui-elem-quote">
  <p>layui - 在每一个细节中，用心与你沟通</p>
</div>
    
  </div>
</div>
  

<div class="layui-footer footer footer-doc">
    <p>
      Copyright &copy; 2021 <a href="../../index.htm" tppabs="http://www.layui.com/">layui.com</a> MIT Licensed</p>
    <p>
      <a href="../../about/disclaimer.html" tppabs="http://www.layui.com/about/disclaimer.html" target="_blank">免责声明</a> 
      <a href="../../about/relatedlinks.html" tppabs="http://www.layui.com/about/relatedlinks.html" target="_blank">友链</a>
      <a href="javascript:;"  site-event="weixinmp">公众号</a>
      <a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=36010902000274"  rel="nofollow">
        <img src="../../../res.layui.com/upload/2021_8/168_1628847893037_87773.png" tppabs="http://cdn.layui.com/upload/2021_8/168_1628847893037_87773.png">
        公网安备 00000000000000号
      </a>
      <a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">ICP备00000000号</a>
    </p>
    <div class="site-union">
      <p class="site-union-desc">
        <span>
          感谢以下平台提供云加速支持
        </span>
      </p>

    </div>
</div>
<script>
window.global = {
  pageType: 'doc'
  ,preview: function(){
    var preview = document.getElementById('LAY_preview');
    return preview ? preview.innerHTML : '';
  }()
};
</script>

<div class="site-tree-mobile layui-hide">
  <i class="layui-icon layui-icon-spread-left"></i>
</div>
<div class="site-mobile-shade"></div>
 
<script src="../../../res.layui.com/layui/dist/layui.js" tppabs="http://res.layui.com/layui/dist/layui.js?t=1632428048260" charset="utf-8"></script>
<script>
layui.config({
  base: '../../../res.layui.com/static/lay/modules/layui/'
  ,version: '1632428048260'
}).use('global');
</script>
 


</body>
</html>